package com.nl.lib_core.tool

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.util.Patterns
import android.webkit.URLUtil
import com.nl.lib_core.BaseApplication
import java.net.MalformedURLException
import java.util.regex.Pattern

/** 作者: wangfei 创建时间: 2021/9/1 包名: com.nl.lib_core.util 类名: NetUtil */
object NetUtil {

  /** 检查网络情况 */
  fun checkNet(): Boolean {
    val context = BaseApplication.instance
    return isWifiConnection(context!!) || isStationConnection(context)
  }

  /** 检查网络情况 网络不好弹出提示框 */
  fun checkNetToast(): Boolean {
    val isNet = checkNet()
    if (!isNet) {
      ToastUtil.showShort("网络不给力哦！")
    }
    return isNet
  }

  /** 判断当前的URL是否有效 */
  fun checkUrlValid(urlString: String): Boolean {
    try {
      return URLUtil.isValidUrl(urlString) && Patterns.WEB_URL.matcher(urlString).matches()
    } catch (e: MalformedURLException) {}
    return false
  }

  /** 校验IP地址 */
  fun checkIpAddress(ipString: String): Boolean {
    if (!Pattern.compile(
            "\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b")
        .matcher(ipString)
        .matches()) {
      return false
    }
    return true
  }

  /**
   * 是否使用基站联网
   *
   * @param context
   * @return
   */
  private fun isStationConnection(context: Context): Boolean {
    try {
      val connectivity =
          context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
      if (connectivity != null) {
        val info = connectivity.activeNetworkInfo
        if (info != null && info.isConnected) {
          if (info.state == NetworkInfo.State.CONNECTED) {
            return true
          }
        }
      }
    } catch (e: Exception) {
      return false
    }
    return false
  }

  /**
   * 是否使用WIFI联网
   *
   * @param context
   * @return
   */
  private fun isWifiConnection(context: Context): Boolean {
    val manager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
    return networkInfo.let { networkInfo?.isAvailable!! && networkInfo.isConnected }
  }

  fun isNetWorkState(context: Context): NetType {
    val manager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val activeNetwork = manager.activeNetworkInfo
    if (activeNetwork != null) {
      if (activeNetwork.isConnected) {
        if (activeNetwork.type == ConnectivityManager.TYPE_WIFI) {
          // Logger.v(TAG, "当前WiFi连接可用 ");
          return NetType.WIFI
        } else if (activeNetwork.type == ConnectivityManager.TYPE_MOBILE) {
          // Logger.v(TAG, "当前移动网络连接可用 ");
          return NetType.NET_4G
        }
      } else {
        // Logger.v(TAG, "当前没有网络连接，请确保你已经打开网络 ");
        return NetType.NO_NET
      }
    } else {
      // Logger.v(TAG, "当前没有网络连接，请确保你已经打开网络 ");
      return NetType.NO_NET
    }
    return NetType.NO_NET
  }

  enum class NetType {
    WIFI,
    NET_4G,
    NO_NET
  }
}
